<template>
	<view>
		<image :src="data" ></image>
		<button type="success" @click="upload"
		style="background-color: #4CD964; color: #FFFFFF; margin-top: 20px;">上传图标</button>
	</view>
</template>

<script>
	export default {
		
		props:{
			data:{
				type: String,
				default() {
					return ''
				}
			}
			
		},
		
		methods:{
			/* 上传图标 */
			upload() {
				new Promise((resolve,reject) => {
					/* 选择本地图标 */
					uni.chooseImage({
						count:1,
						success(res) {
							if(res.tempFilePaths.length >0) {
								uni.showLoading({
									title:"上传中..."
								})
								
								let filePath = res.tempFilePaths[0]
								console.log(filePath,'filePath--------------');
								/* 上传图标到云存储空间 */
								uniCloud.uploadFile({
									filePath:filePath,
									cloudPath: Date.now() + '.jpg',
									success(res) {
										console.log(res);
										resolve(res.fileID)
									},
									fail(err) {
										reject(new Error('fail_upload'))
									},
									complete() {
										uni.hideLoading()
									}
								})
							}
						}
					})
				}).then(res => {
					uniCloud.getTempFileURL({
						fileList:[res],
						success:(res) =>{
							
							this.$emit('setData',res.fileList[0].tempFileURL)
						},
						fail: () => {
							uni.showModal({
								content:'获取临时链接失败',
								showCancel:false
							})
						}
					})
				})
			},
		}
	}
</script>

<style>
</style>
